/**
 * 
 */
package xx.services.teach;

import java.sql.Timestamp;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.sql.RowSet;
import javax.ws.rs.FormParam;
import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.PUT;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.QueryParam;
import javax.ws.rs.core.Context;

import xx.services.Passport;
import xx.services.util.Constants;
import xx.teach.entity.Account;
import xx.teach.entity.Chat;
import xx.teach.entity.Group;
import xx.teach.entity.Job;
import xx.teach.entity.Meno;
import xx.teach.entity.Schedule;

/**
 * @author Administrator 2014-3-18
 */
@Path(Constants.RESTFUL_ROOT_PATH + "/teach/group")
public class GroupRest extends Passport
{

    /**
     * @param request
     */
    public GroupRest(@Context
    HttpServletRequest request)
    {
        super(request);
        // TODO Auto-generated constructor stub
    }

    /**
     * 根据groupId分页获取account列表
     * 
     * @param groupId
     * @return
     */
    @GET
    @Path("/{group_id}/account")
    public String getAccountByGroupId(@PathParam("group_id")
    long groupId)
    {
        try
        {
            Group group = new Group(qr, owner);
            List<Account> accountList = group.setGroupId(groupId).getAccountByGroupId(pageSize, pageNo, condition, orderField);
            return ok(accountList);
        }
        catch (Exception e)
        {
            e.printStackTrace();
            return error(e.getMessage());
        }
    }

    /**
     * 获取所有班级列表
     * 
     * @return
     */
    @GET
    public String getAllGroup()
    {
        try
        {
            Group group = new Group(qr, owner);
            List<Group> groupList = group.getAllGroup();
            return ok(groupList);
        }
        catch (Exception e)
        {
            e.printStackTrace();
            return error(e.getMessage());
        }
    }

    /**
     * 根据groupId分页获取meno列表
     * 
     * @param groupId
     * @return
     */
    @GET
    @Path("/{group_id}/memo")
    public String getMenoByGroupId(@PathParam("group_id")
    long groupId)
    {
        try
        {
            Group group = new Group(qr, owner);
            group.setGroupId(groupId);
            List<Meno> menoList = group.getMenoByGroupId(pageSize, pageNo, condition, orderField);
            return ok(menoList);
        }
        catch (Exception e)
        {
            e.printStackTrace();
            return error(e.getMessage());
        }
    }

    /**
     * 根据groupId，为班级中的每个学生插入时间表
     * 
     * @param groupId
     * @param classTime
     * @return
     */
    @POST
    @Path("/{group_id}/schedule")
    public String addScheduleByGroupId(@PathParam("group_id")
    long groupId, @FormParam("class_time")
    String classTime)
    {
        try
        {
            Schedule schedule = new Schedule(qr, owner);
            Timestamp classTm = Timestamp.valueOf(classTime);
            int count = schedule.setGroupId(groupId).setClassTime(classTm).insertByGroupId();
            return ok(count);
        }
        catch (Exception e)
        {
            e.printStackTrace();
            return error(e.getMessage());
        }
    }

    /**
     * 根据groupId分页获取schedule列表
     * 
     * @param groupId
     * @return
     */
    @GET
    @Path("/{group_id}/schedule")
    public String getScheduleByGroupId(@PathParam("group_id")
    long groupId)
    {
        try
        {
            Group group = new Group(qr, owner);
            List<Schedule> scheduleList = group.getScheduleByGroupId(pageSize, pageNo, condition, orderField);
            return ok(scheduleList);
        }
        catch (Exception e)
        {
            e.printStackTrace();
            return error(e.getMessage());
        }
    }

    /**
     * 根据groupId分页获取chat列表
     * 
     * @param groupId
     * @return
     */
    @GET
    @Path("/{group_id}/chat")
    public String getChatByGroupId(@PathParam("group_id")
    long groupId)
    {
        try
        {
            Group group = new Group(qr, owner);
            RowSet chatList = group.setGroupId(groupId).getChatByGroupId(pageSize, pageNo, condition, orderField);
            return ok(chatList);
        }
        catch (Exception e)
        {
            e.printStackTrace();
            return error(e.getMessage());
        }
    }

    /**
     * 新增group
     * 
     * @param groupname
     * @return
     */
    @POST
    public String addGroup(@FormParam("groupname")
    String groupname)
    {
        try
        {
            Group group = new Group(qr, owner);
            long groupId = group.setGroupname(groupname).insert();
            return ok(groupId);
        }
        catch (Exception e)
        {
            e.printStackTrace();
            return error(e.getMessage());
        }
    }

    /**
     * 更新group信息
     * 
     * @param groupId
     * @param groupname
     * @return
     */
    @PUT
    @Path("/{group_id}")
    public String updateGroup(@PathParam("group_id")
    long groupId, @QueryParam("groupname")
    String groupname)
    {
        try
        {
            Group group = new Group(qr, owner);
            int count = group.setGroupId(groupId).setGroupname(groupname).update();
            return ok(count);
        }
        catch (Exception e)
        {
            e.printStackTrace();
            return error(e.getMessage());
        }
    }

    /**
     * 新增memo
     * 
     * @param groupId
     * @param content
     * @return
     */
    @POST
    @Path("/{group_id}/memo")
    public String addMemo(@PathParam("group_id")
    long groupId, @FormParam("content")
    String content)
    {
        try
        {
            Meno memo = new Meno(qr, owner);
            long memoId = memo.setGroupId(groupId).setContent(content).insert();
            return ok(memoId);
        }
        catch (Exception e)
        {
            e.printStackTrace();
            return error(e.getMessage());
        }
    }

    /**
     * 新增chat
     * 
     * @param groupId
     * @param content
     * @return
     */
    @POST
    @Path("/{group_id}/chat")
    public String addChat(@PathParam("group_id")
    long groupId, @FormParam("account_id")
    long accountId, @FormParam("content")
    String content, @FormParam("attach_path")
    String attachPath)
    {
        try
        {
            Chat chat = new Chat(qr, owner);
            long chatId = chat.setGroupId(groupId).setAccountId(accountId).setContent(content).setAttachPath(attachPath).insert();
            return ok(chatId);
        }
        catch (Exception e)
        {
            e.printStackTrace();
            return error(e.getMessage());
        }
    }

    /**
     * 新增account
     * 
     * @param groupId
     * @param content
     * @return
     */
    @POST
    @Path("/{group_id}/account")
    public String addAccount(@PathParam("group_id")
    long groupId, @FormParam("phone")
    String phone, @FormParam("password")
    String password, @FormParam("type")
    int type, @FormParam("teacher_id")
    long teacherId, @FormParam("name")
    String name)
    {
        try
        {
            Account account = new Account(qr, owner);
            account.setGroupId(groupId);

            if (phone != null && !phone.isEmpty())
                account.setPhone(phone);

            if (password != null && !password.isEmpty())
                account.setPassword(password);
            else
                // 使用name作为密码
                account.setPassword(name);

            if (type == 0)
                account.setType(2);
            else
                account.setType(type);

            account.setTeacherId(teacherId).setName(name);

            long accountId = account.insert();
            return ok(accountId);
        }
        catch (Exception e)
        {
            e.printStackTrace();
            return error(e.getMessage());
        }
    }

    /**
     * 新增job
     * 
     * @param groupId
     * @param content
     * @return
     */
    @POST
    @Path("/{group_id}/job")
    public String addJob(@PathParam("group_id")
    long groupId, @FormParam("type")
    int type, @FormParam("name")
    String name, @FormParam("content")
    String content, @FormParam("attach_path")
    String attachPath, @FormParam("teacher_id")
    long teacherId)
    {
        try
        {
            if (type == 0)
                return error("确实job type值");

            Job job = new Job(qr, owner);
            job.setGroupId(groupId).setType(type);

            if (name != null && !name.isEmpty())
                job.setName(name);

            if (content != null && !content.isEmpty())
                job.setContent(content);

            if (attachPath != null && !attachPath.isEmpty())
                job.setAttachPath(attachPath);

            job.setTeacherId(teacherId);

            long jobId = job.insert();
            return ok(jobId);
        }
        catch (Exception e)
        {
            e.printStackTrace();
            return error(e.getMessage());
        }
    }
}
